<?php
namespace app\common\model\newmodel;

use think\Model;

class Accessories extends Model
{
    protected $table = 'accessories';
    protected $pk = 'id';


    public function skuvalue()
    {
        return $this->hasMany('app\\common\\model\\newmodel\\AccessorySku','accessory_id');

    }
    /**
     * 没有分页的配件列表
     */
    public static function accessoryList()
    {
        $query=self::select()->toArray();
        return ['code'=>1,'msg'=>$query];
    }

    /**
     * 配件列表
     */
    public static function getAccessoryList($page,$pageSize,$keyword='')
    {
        $query=self::with(['skuvalue'])->where('id','>',0);
        if($keyword){
            $query=$query->where('accessory_name','like', "%$keyword%");
        }
        $query=$query->paginate([
                        'list_rows' => $pageSize,
                        'page' => $page,
                    ]);
        return [
            'code' => 1,
            'msg' => 'success',
            'data' => $query
        ];
    }

    /**
     * 添加配件
     */
    public static function addAccessory($data)
    {
        if (empty($data['accessory_name'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }
        try {
            $accessory = new self();
            $accessory->accessory_name = $data['accessory_name'];
            $accessory->describe_info = $data['describe_info'] ?? '';
            
            $accessory->update_time=time();
            $accessory->create_time=time();
            $accessory->save();
            
            return ['code' => 1, 'msg' => '配件添加成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '配件添加失败：' . $e->getMessage()];
        }
    }

    /**
     * 修改配件
     */
    public static function updateAccessory($data)
    {
        if (empty($data['id']) || empty($data['accessory_name'])) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $accessory = self::get($data['id']);
        if (!$accessory) {
            return ['code' => 0, 'msg' => '配件不存在'];
        }

        try {
            $accessory->accessory_name = $data['accessory_name'];
            $accessory->describe_info = $data['describe_info'] ?? '';
            $accessory->update_time=time();
            $accessory->save();
            
            return ['code' => 1, 'msg' => '配件修改成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '配件修改失败：' . $e->getMessage()];
        }
    }

    /**
     * 删除配件
     */
    public static function deleteAccessory($id)
    {
        if (empty($id)) {
            return ['code' => 0, 'msg' => '参数不完整'];
        }

        $accessory = self::get($id);
        if (!$accessory) {
            return ['code' => 0, 'msg' => '配件不存在'];
        }

        try {
            $accessory->delete();
            return ['code' => 1, 'msg' => '配件删除成功'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => '配件删除失败：' . $e->getMessage()];
        }
    }
}